//
//  DWDComposeView.swift
//  dwdWb
//
//  Created by Dengdwd on 15/10/18.
//  Copyright © 2015年 dwd. All rights reserved.
//

import UIKit
import pop



class DWDComposeView: UIView {

    private lazy var btns:[DWDComBtn] = [DWDComBtn]()

    var vc:UIViewController?


    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setUpUI() {
        self.frame = CGRectMake(0, 0, SCREENW, SCREENH)
        let backImage = UIImageView()
        backImage.image = screenImage().applyLightEffect()
        backImage.size = CGSizeMake(SCREENW, SCREENH)
        addSubview(backImage)
        setUpBtns()
        let logoImage = UIImageView(image: UIImage(named: "compose_slogan"))
        logoImage.centerX = self.centerX
        logoImage.y = 100
        addSubview(logoImage)
        
    }
    private func screenImage()->UIImage {
        let window = UIApplication.sharedApplication().keyWindow
        UIGraphicsBeginImageContextWithOptions(CGSize(width: SCREENW, height: SCREENH), false, 0)
//        self.window?.layer.renderInContext(<#T##ctx: CGContext##CGContext#>)
        window!.drawViewHierarchyInRect(window!.bounds, afterScreenUpdates: false)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
    
    private func setUpBtns() {
        let path = NSBundle.mainBundle().pathForResource("compose.plist", ofType: nil)
        let array = NSArray(contentsOfFile: path!)
        for i in  0 ..< array!.count {
            let btn = DWDComBtn()
            btn.addTarget(self, action: "clickComposeBtn:", forControlEvents: UIControlEvents.TouchUpInside)
            let col = i % 3
            let row = i / 3
            let margin: CGFloat = (SCREENW - 3 * COM_BTN_W) / 4
            btn.frame = CGRectMake(margin + CGFloat(col) * (margin + COM_BTN_W) , CGFloat(row) * (margin + COM_BTN_H) + SCREENH, COM_BTN_W, COM_BTN_H)
            let info = array![i] as! [String:AnyObject]
            btn.setTitle(info["title"] as? String, forState: UIControlState.Normal)
            btn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
            btn.setImage(UIImage(named: info["icon"] as! String), forState: UIControlState.Normal)
            btns.append(btn)
            addSubview(btn)
        }
    }
    @objc func clickComposeBtn(btn:DWDComBtn) {
       
        UIView.animateWithDuration(1, animations: { () -> Void in
            for value in self.btns {
                if value == btn {
                value.transform = CGAffineTransformMakeScale(2, 2)
                }else{
                value.transform = CGAffineTransformMakeScale(0.25, 0.25)
                }
                value.alpha = 0.1
            }
            }, completion: { (finish) -> Void in
   
                let vc = DWDComposeViewController()
                let nav = UINavigationController(rootViewController: vc)
                self.vc?.presentViewController(nav, animated: true, completion:{
                self.removeFromSuperview()
                
                })
                
        })
        
        
        

    }
    func show(target:UIViewController) {
        target.view.addSubview(self)
        self.vc = target
        for (index,value) in btns.enumerate()  {
           anim(index, value: value, offset: value.centerY - 400)
        }
    }
    private func anim(index:NSInteger, value:UIButton, offset:CGFloat) {
        let anim = POPSpringAnimation(propertyNamed: kPOPViewCenter)
        anim.toValue = NSValue(CGPoint:CGPoint(x: value.centerX, y: offset))
        anim.springBounciness = 10
        anim.springSpeed = 10
        anim.beginTime =  CACurrentMediaTime() + Double(index) * 0.025
        value.pop_addAnimation(anim, forKey: nil)
    
    }
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        dismiss()
    }
    private func dismiss() {
        for (index,value) in btns.reverse().enumerate() {
        anim(index, value: value, offset: centerY + 400)
        }
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(0.4 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) { () -> Void in
            self.removeFromSuperview()
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
    }
}
